/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DBTransmit.TableAdminRecord;
import HibernateUtil.HibernateUtil;
import POJO.Adminlist;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.SecretKey;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import ticketbooking.Crypto;
import ticketbooking.EncryptedData;
import ticketbooking.Utils;

/**
 *
 * @author LEHOANGKHANH
 */
public class DAO_Admin {
    public static List<TableAdminRecord> LoadDS(SecretKey key){
            List<Adminlist> ds = null;
            Session session = HibernateUtil.getSessionFactory().openSession();
            try {
                    String sql = "from Adminlist";
                    Query query = session.createQuery(sql);
                    ds = query.list();

            } catch(HibernateException ex) {
                    System.err.println(ex);
            }
            List<TableAdminRecord> kq = new ArrayList<TableAdminRecord>();
            for(int i=0; i<ds.size(); i++){
                try {
                    TableAdminRecord record = new TableAdminRecord();
                    EncryptedData name = (EncryptedData)Utils.toObj(ds.get(i).getUsername());
                    EncryptedData pass = (EncryptedData)Utils.toObj(ds.get(i).getPass());
                    record.setUserName(Crypto.decryptTextWithAES(name, key));
                    record.setPassWord(Crypto.decryptTextWithAES(pass, key));
                    kq.add(record);
                } catch (Exception ex) {
                    Logger.getLogger(DAO_Admin.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            return kq;
    }
    public static TableAdminRecord LoadRecord(String name, SecretKey key){
        //code
        return null;
    }
    public static boolean Add(TableAdminRecord record, SecretKey key){
        try {
            EncryptedData name = Crypto.encryptTextWithAES(record.getUserName(), key);
            EncryptedData pass = Crypto.encryptTextWithAES(record.getPassWord(), key);
            
            Adminlist admin = new Adminlist();
            admin.setUsername(Utils.toByte(name));
            admin.setPass(Utils.toByte(pass));
            Session ss = HibernateUtil.getSessionFactory().openSession();
            boolean kq = true;
            Transaction tran = null;
            try {
                tran = ss.beginTransaction();
                ss.save(admin);
                tran.commit();
            }
            catch( HibernateException ex) {
                tran.rollback();
                System.err.println(ex);
                kq = false;
            }
            return kq;
        } catch (Exception ex) {
            Logger.getLogger(DAO_Admin.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    public static boolean Update(TableAdminRecord record, SecretKey key){
        try {
            EncryptedData name = Crypto.encryptTextWithAES(record.getUserName(), key);
            EncryptedData pass = Crypto.encryptTextWithAES(record.getPassWord(), key);
            
            Adminlist admin = new Adminlist();
            admin.setUsername(Utils.toByte(name));
            admin.setPass(Utils.toByte(pass));
            Session ss = HibernateUtil.getSessionFactory().openSession();
            boolean kq = true;
            Transaction tran = null;
            try {
                tran = ss.beginTransaction();
                ss.update(admin);
                tran.commit();
            }
            catch( HibernateException ex) {
                tran.rollback();
                System.err.println(ex);
                kq = false;
            }
            return kq;
        } catch (Exception ex) {
            Logger.getLogger(DAO_Admin.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    public static boolean Delete(String adminame, SecretKey key){
        try {        
            EncryptedData en = Crypto.encryptTextWithAES(adminame, key);
            Session ss = HibernateUtil.getSessionFactory().openSession();
            boolean state = true;
            Transaction trans = ss.beginTransaction();
            try {
                Adminlist admin = new Adminlist();
                admin = (Adminlist)ss.get(Adminlist.class, Utils.toByte(en));
                ss.delete(admin);
                trans.commit();
            }
            catch ( HibernateException ex) {
                System.err.println(ex);
                trans.rollback();
                state = false;
            }
            return state;
        } catch (Exception ex) {
            Logger.getLogger(DAO_Admin.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
}
